<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /*
        高级函数：以函数为参数或者返回值的函数，称为高级函数
        回调函数：当一个函数内容执行完毕后调用的函数
        */
        function add(a, b, callback) {
            // if (callback) {
            //     return callback(a + b)
            // } else {
            //     return a + b
            // }

            var c = a + b;
            callback && (c = callback(c));
            return c;
        }

        console.log(add(1, 2)); // 3
        console.log(add(1, 2, mul)); // 6
        console.log(add(1, 2, function (a) {
            return a * 2
        })); // 6

        function mul(a) {
            return a * 2
        }

        // 节流也是高级函数 

        function throttle(fnc, wait) {
            var timer = null;
            return function () {
                if (!timer) {
                    timer = setTimeout(function () {
                        fnc();
                        timer = null;
                    }, wait)   
                }
            }
        }

        // 防抖

        function debounce(fnc, wait) {
            var timer = null;
            return function () {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function () {
                    fnc();
                    timer = null;
                }, wait)
            }
        }
    </script>
</body>
</html>